package com.woniu.market_stall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.woniu.market_stall.entity.Role;
import com.woniu.market_stall.mapper.RoleMapper;
import com.woniu.market_stall.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RoleService {

    @Autowired
    RoleMapper roleMapper ;


    public PageInfo<Role> queryByPage(Integer now, Integer size, String name){
        PageHelper.startPage(now,size);
        List<Role> roles = roleMapper.selcetRoleByName(name);
        return new PageInfo<>(roles);
    }

    public Role queryRoleByKey(Integer id){
        return roleMapper.selectByPrimaryKey(id);
    }
    public int add(Role role){
        return roleMapper.insert(role);
    }

    public int modify(Role role){
        return roleMapper.updateByPrimaryKey(role);
    }

    @Transactional
    public int removeBykey(Integer id){
        //1. 解除用户绑定的角色
        roleMapper.deleteManagerRoleByRoleId(id);
        //2.解除 权限绑定的角色
        roleMapper.deleteRolePermissionByRoleId(id);
        //3.删除角色
        int i = roleMapper.deleteByPrimaryKey(id);
        return i ;
    }

    @Transactional
    public void authority(RoleVo roleVo) {
        // 1. 先删除 当前角色的所有权限
        roleMapper.deleteRolePermissionByRoleId(roleVo.getId());
        // 2. 插入新的权限
        roleMapper.insertPermissionByRole(roleVo.getId(),roleVo.getPerCheckedIds());
        roleVo.setInsertTime(new Date());
        roleVo.setUpdateTime(new Date());
        roleVo.setIsDelete(0);
    }

    public List<Role> queryAllRole() {
        return roleMapper.selectAll();
    }




}
